import axios from 'axios'
import util from '@/util/util.js'
const methods = {
    //ajax请求
    reqDelUser (id) {
        
        this.$confirm('确认删除？', '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.$store.commit('showLoading');
            axios.get('/index/User/delUser', {
                params: {
                    id: id
                }
            }).then((datas) => {
                var data = datas.data;
                if (data.status === 0) {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                }
                else {

                }
                this.listenRoute();
                
            }, (err) => {
                
                console.log(err)
                this.listenRoute();
            })
        }).catch(() => {
            
            this.$message({
                type: 'info',
                message: '已取消删除'
            })
        })
    },
    confirmEdit (form) {
        var imgUrl = util.uploadFile(form.userAvatar, this);
        this.$store.commit('showLoading');
        axios.get('/index/User/updateUser', {
            params: {
                id: form.id,
                userAvatar: imgUrl,
                userName: form.userName,
                isExpert: form.isExpert ? 1 : 0,
                haveMoney: form.haveMoney ? form.haveMoney : 0,
                isBlack: form.isBlack ? 1 : 0
            }
        }).then((datas) => {
            var data = datas.data;
            this.$message({
                message: '更新成功',
                type: 'success'
            });
          
            this.listenRoute();
        }, (err) => {
            this.$message.error('更新失败，服务器挂了');
          
            this.listenRoute();
            console.log(err)
        })
    },
    getUser () {
       
        this.$store.commit('showLoading');
        axios.get('/index/User/getUser', {
            params: {
                page: this.page,
                dataNum: this.dataNum,
                keyName: this.keyName,
                keyWord: this.keyWord
            }
        }).then((datas) => {
            var data = datas.data;
            if (data.status === 0) {
                this.dataTotal = data.data.dataTotal;
                this.allUser = data.data.allNote;
            }
            else {

            }
            this.$store.commit('hiddenLoading')
        }, (err) => {   
            console.log(err);
            this.$store.commit('hiddenLoading')
            this.$message.error('服务器挂了!')
        })
    },
    //监听路由
    listenRoute () {
        var route = this.$route;
        var query = route.query;
        
        var page = query.page ? query.page : 1;
        var keyName = query.keyName ? query.keyName : '';
        var keyWord = query.keyWord ? query.keyWord : '';

        this.page = page;
        this.keyName = keyName;
        this.keyWord = keyWord;

        this.getUser();
    },
    //行为
    refresh () {
        this.listenRoute();
    },
    choosePage (index) {
        var route = this.$route;
        var routePath = route.name;
        var query = route.query;
        query.page = index;
        this.$router.push({
            path: routePath,
            query: query
        });
        this.listenRoute();
        
    },
    searchUser (param) {
        console.log(param)
        this.keyName = param.searchKind;
        this.keyWord = param.searchKeyWord;
        if (this.keyWord === '是') {
            this.keyWord = 1;
        }
        else if (this.keyWord === '否') {
            this.keyWord = 0;
        }
        else {
            
        }
        this.listenRoute();
    },
    lookUser(index) {
        var user = this.allUser[index];
        this.$refs.dialog.show(user);
    },
    delUser (index) {
        var user = this.allUser[index];
        this.reqDelUser(user.id);
    },
}
export default methods